package com.xinai.dao;

import java.util.List;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.xinai.util.DBUtil;
import com.xinai.vo.StockRemove;

/**
 * Title: StockRemoveDao Description: 货架移位DAO(仓位管理)
 *
 * @author LYJ
 *
 */
public class StockRemoveDao extends BaseDao {
    private static final String TABLE = "stockRemove";

    public StockRemoveDao(Context context) {
        super(context);
    }

    public List<StockRemove> getList() {
        return callBack(TYPE_READ, new DaoCallBack<List<StockRemove>>() {
            @Override
            public List<StockRemove> invoke(SQLiteDatabase conn) {
                String sql = " select * from stockRemove ";
                cursor = conn.rawQuery(sql, null);
                if (cursor == null) {
                    return null;
                }
                List<StockRemove> sr = DBUtil.cursor2VOList(cursor, StockRemove.class);
                return sr;
            }
        });
    }

    public Integer getStockRemoveTotalCount() {
        return callBack(TYPE_READ, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                int count = 0;
                String sql = " select sum(qty) qty from stockRemove ";
                cursor = conn.rawQuery(sql, null);
                if (cursor == null) {
                    return null;
                }
                while (cursor.moveToNext()) {
                    count = cursor.getInt(cursor.getColumnIndex("qty"));
                }
                return count;
            }
        });
    }

    public StockRemove find(final StockRemove stockRemove) {
        return callBack(TYPE_READ, new DaoCallBack<StockRemove>() {
            @Override
            public StockRemove invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, " DeptID = ? and storageID = ? and productID = ? and colorID = ? and sizesID = ? ", new String[] {stockRemove.getDeptId(), stockRemove.getStorageID(), stockRemove.getProductID(), stockRemove.getColorID(), stockRemove.getSizesID()}, null, null, null);
                if (cursor.moveToFirst() == false) {
                    return null;
                }
                StockRemove stockRemove = new StockRemove();
                stockRemove.setId(cursor.getInt(cursor.getColumnIndex("id")));
                stockRemove.setDeptId(cursor.getString(cursor.getColumnIndex("DeptID")));
                stockRemove.setBarcode(cursor.getString(cursor.getColumnIndex("Barcode")));
                stockRemove.setSupplyNo(cursor.getString(cursor.getColumnIndex("supplyNo")));
                stockRemove.setProductNo(cursor.getString(cursor.getColumnIndex("productNo")));
                stockRemove.setProductName(cursor.getString(cursor.getColumnIndex("productName")));
                stockRemove.setColorName(cursor.getString(cursor.getColumnIndex("colorName")));
                stockRemove.setSizesName(cursor.getString(cursor.getColumnIndex("sizesName")));
                stockRemove.setStorageNo(cursor.getString(cursor.getColumnIndex("storageNo")));
                stockRemove.setStorageID(cursor.getString(cursor.getColumnIndex("storageID")));
                stockRemove.setProductID(cursor.getString(cursor.getColumnIndex("productID")));
                stockRemove.setColorID(cursor.getString(cursor.getColumnIndex("colorID")));
                stockRemove.setSizesID(cursor.getString(cursor.getColumnIndex("sizesID")));
                stockRemove.setQty(cursor.getInt(cursor.getColumnIndex("qty")));
                stockRemove.setType(cursor.getString(cursor.getColumnIndex("type")));
                return stockRemove;
            }
        });
    }

    public int update(final StockRemove stockRemove) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL(" update stockRemove set qty = ? where id = ? ", new Object[] {stockRemove.getQty(), stockRemove.getId()});
                return 1;
            }
        });
    }

    public int insert(final StockRemove stockRemove) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL(
                        " insert into stockRemove(DeptID,DeptName,Barcode,supplyNo,productNo,productName,colorName,sizesName,storageNo,storageID,productID,colorID,sizesID,qty,type) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
                        new Object[] {stockRemove.getDeptId(), stockRemove.getDeptName(), stockRemove.getBarcode(), stockRemove.getSupplyNo(), stockRemove.getProductNo(), stockRemove.getProductName(), stockRemove.getColorName(), stockRemove.getSizesName(), stockRemove.getStorageNo(),
                                stockRemove.getStorageID(), stockRemove.getProductID(), stockRemove.getColorID(), stockRemove.getSizesID(), stockRemove.getQty(),stockRemove.getType()});
                return 1;
            }
        });
    }

    public int delete(final String id) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                return conn.delete(TABLE, " id = ? ", new String[] {id});
            }
        });
    }

}
